home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / NEWSOFT / AUGUST / WORKDISC / !Forthmacs / lib / srec < prev    next >
Text File  |  1997-04-17  |  2KB  |  67 lines

  1. \ < Convert a binary file to a file containing S-records.
  2. \
  3. \ To use from the command line:
  4. \
  5. \    forth srec.fth -s "to-srecs input-filename output-filename"
  6. \
  7. \ to-srecs  \ input-filename output-filename  ( -- )
  8. \    "input-filename" is the name of a file containing binary data
  9. \    (just data; no headers or anything else).  to-srec creates a
  10. \    file named "output-filename", and stores in it the S-record
  11. \ >    representation of the binary data in the input file.
  12.  
  13. h# 20 constant max-srec
  14.  
  15. variable checksum
  16. variable load-address
  17.  
  18. create outchars ," 0123456789ABCDEF"
  19.  
  20. : fcr        ( fd -- )    newline-pstring count rot fputs  ;
  21.  
  22. \ Split a longword into its constituent bytes
  23. : lbsplit    ( l -- lowbyte lmbtye hmbyte highbyte )
  24.     dup  h# ff and  swap 8 rshift
  25.     dup  h# ff and  swap 8 rshift
  26.     dup  h# ff and  swap 8 rshift
  27.          h# ff and ;
  28.  
  29. : .nibble    ( n -- )  \ Output a hex nibble
  30.      h# 0f and  outchars char+ swap chars+ c@  ofd @ fputc ;
  31.  
  32. : .byte    ( byte -- ) \ output a hex byte and accumulate the checksum
  33.     dup checksum +!
  34.     dup 4 rshift .nibble .nibble ;
  35.  
  36. : .addr    ( l.address -- )  \ Output a 24-bit hex address and checksum
  37.     lbsplit drop .byte .byte .byte ;
  38.  
  39. : .data    ( address count -- )  \ Output the count data bytes starting at addr
  40.     bounds  do i c@ .byte  loop ;
  41.  
  42. : .srec ( mem-address count -- ) \ Output a data S-record
  43.     s" S2" ofd @ fputs  0 checksum !
  44.     dup 4 + .byte        ( load-address mem-address count )  \ output the count field
  45.     load-address @ .addr    ( address count )
  46.     dup load-address +!
  47.     .data
  48.     checksum @  not  .byte
  49.     ofd @ fcr ;
  50.  
  51. max-srec buffer: ibuf
  52.  
  53. \ Output data S-records until the count is exhausted
  54. : .srecs  ( load-adr -- )
  55.     load-address !
  56.     s" S204000000FB" ofd @ fputs  ofd @ fcr   \ Header record
  57.     begin    ibuf max-srec
  58.         ifd @ fgets  dup 0>
  59.     while        ( actual-length )
  60.         ibuf swap .srec
  61.     repeat
  62.     drop
  63.     s" S804000000FB" ofd @ fputs  ofd @ fcr    \ Trailer record
  64.     ofd @ fclose ;
  65. : to-srecs  \ input-filename output-filename  ( -- )
  66.     reading  writing  0 .srecs ;
  67.